﻿using Microsoft.AspNetCore.Mvc.Rendering;
using Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Code {
    public static class BaseCode {
        #region 加密
        public static string MD5Encrypt64(this string value) {
            using (var md5 = MD5.Create()) {
                var result = md5.ComputeHash(Encoding.UTF8.GetBytes(value));

                var strResult = Convert.ToBase64String(result);

                return strResult;
            }
        }
        #endregion

        #region 将泛型转成json字符串
        /// <summary>
        /// 把泛型转换成json字符串
        /// </summary>
        /// <param name="obj">实体模型</param>
        /// <param name="DateFormatString">设置时间格式，默认为yyyy-MM-dd HH:mm:ss</param>
        /// <param name="loopHandling">Newtonsoft.Json.ReferenceLoopHandling转换设置，默认为不循环引用</param>
        /// <returns>string</returns>
        public static string ToJsonString<T>(this T obj, string DateFormatString = "yyyy-MM-dd HH:mm:ss", ReferenceLoopHandling loopHandling = ReferenceLoopHandling.Ignore) {
            JsonSerializerSettings jsSettings = new JsonSerializerSettings();
            //jsSettings.ReferenceLoopHandling = loopHandling;
            JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() => {
                jsSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
                jsSettings.DateFormatString = DateFormatString;
                jsSettings.ReferenceLoopHandling = loopHandling;
                jsSettings.NullValueHandling = NullValueHandling.Ignore;

                return jsSettings;
            });

            return JsonConvert.SerializeObject(obj, jsSettings);
        }
        #endregion

        #region 生成表头
        public static List<Columns> GetColumns(this Type t) {
            List<Columns> list = new List<Columns>();
            string typeName = "";
            bool flag = false;

            PropertyInfo[] pros = t.GetProperties();

            foreach (var p in pros) {
                Columns c = new Columns {
                    field = p.Name
                };
                var attrs = p.GetCustomAttributes(false);

                if (attrs.Count() > 0) {
                    foreach (var attr in attrs) {
                        typeName = attr.GetType().Name;
                        if (typeName.Equals("DisplayNameAttribute")) {
                            c.title = (attr as System.ComponentModel.DisplayNameAttribute).DisplayName;
                            flag = true;
                        }

                        if (typeName.Equals("ListAttribute")) {
                            var listAttr = (attr as ListAttribute);
                            int type = 0;

                            if (listAttr.Name.Equals("Show")) {
                                int.TryParse(listAttr.Value.ToString(), out type);

                                c.show = type;
                            }

                            if (listAttr.Name.Equals("Order")) {
                                int.TryParse(listAttr.Value.ToString(), out type);

                                c.order = type;
                            }

                            if (listAttr.Width > 0) {
                                c.width = listAttr.Width;
                            }

                            if ((listAttr.Templet + "").Length > 0) {
                                c.templet = listAttr.Templet;
                            }
                        }
                    }

                    if (flag) {
                        list.Add(c);
                        flag = false;
                    }
                }
            }

            return list;
        }
        #endregion

        #region 枚举转List<T>
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static List<ItemList> GetEnumByList<T>(ItemList item = null) {
            List<ItemList> list = new List<ItemList>();

            Type t = typeof(T);

            if (item != null) {
                list.Add(item);
            }

            foreach (var e in Enum.GetValues(t)) {
                list.Add(new ItemList {
                    text = e.ToString(),
                    value = Convert.ToInt32(e)
                });
            }

            return list;
        }
        #endregion

        public static string GetRandomCode(int length) {
            StringBuilder sb = new StringBuilder();

            for (var i = 0; i < length; i++) {
                Random r = new Random(Guid.NewGuid().GetHashCode());

                sb.Append(r.Next(0, 10));
            }

            return sb.ToString();
        }
    }
}
